using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
namespace Services
{

    public partial class Stock_VoucherInService
    {
       readonly IStock_VoucherInEntity entity;

       public Stock_VoucherInService()
       {
           this.entity = new Stock_VoucherInEntity();

       }
       #region SQLCmd

       //private string StrGetVoucherNo = @"SELECT ('{0}-' + RIGHT('00000'+CONVERT(varchar(6),(ISNULL(MAX(RIGHT(Stock_VoucherIn.VoucherInNo,6)),0)+1)),6)) AS VoucherInNo FROM Stock_VoucherIn";
       private string StrGetVoucherNo = @"SELECT case when (SELECT COUNT(*) FROM Stock_VoucherIn) > 0 then (SELECT TOP 1 '{0}' + CONVERT(varchar(10),SUBSTRING(VoucherInNo ,4 , LEN(VoucherInNo)-3)+1) 
FROM Stock_VoucherIn ORDER BY VoucherInNo DESC) else '{0}1' end as VoucherInNo";
       private string StrGetVoucher = @"select SV.ID,SV.VoucherInNo,SV.VoucherDate,SV.Amount,SV.VAT,SV.TotalAmount,SV.Note,
                                            PW.Name as StockName,
                                            SM.Name as Input_MethodName,
                                            PM.Name as PaymentMethodName,
                                            S.Name as EmployeeName
                                            from Stock_VoucherIn SV
                                            left join Product_Warehouse PW on PW.ID=SV.WarehouseID
                                            left join Stock_Method SM on SM.ID=SV.Input_MethodID
                                            left join Payment_Method PM on PM.ID=SV.Payment_MethodID
                                            left join Staff S on S.ID=SV.EmployeeID where SV.IsDelete=0 and {0}";
       
       #endregion
       #region Main Method

       /// <summary>
       /// Save 
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public long Save(object _model)
       {
           try
           {
               var id = long.Parse(_model.GetType().GetProperty("ID").GetValue(_model, null).ToString());
               if (id == 0)
               {
                   return entity.Save((CoreData.Stock_VoucherIn)_model, Table.Stock_VoucherIn.ToString());
               }
               else
               {
                   return entity.Update((CoreData.Stock_VoucherIn)_model, Table.Stock_VoucherIn.ToString());
               }
           }
           catch { return -1; }
       }

       /// <summary>
       /// Get by ID
       /// </summary>
       /// <param name="_id"></param>
       /// <returns></returns>
       public CoreData.Stock_VoucherIn GetByID(long _id)
       {
           try
           {
               return entity.GetById(_id, Table.Stock_VoucherIn.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Stock_VoucherIn> GetList()
       {
           try
           {
               return entity.GetAll(Table.Stock_VoucherIn.ToString()).ToList();
           }
           catch { return null; }

       }

       /// <summary>
       /// Get List with Linq
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Stock_VoucherIn> GetListByLINQ(Func<CoreData.Stock_VoucherIn, Boolean> _where)
       {
           try
           {
               return entity.GetMany(_where, Table.Stock_VoucherIn.ToString()).ToList();
           }
           catch { return null; }
       }
       /// <summary>
       /// Get One with Linq
       /// </summary>
       /// <returns></returns>
       public CoreData.Stock_VoucherIn GetOneByLINQ(Func<CoreData.Stock_VoucherIn, Boolean> _where)
       {
           try
           {
               return entity.Get(_where, Table.Stock_VoucherIn.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List with search string
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Stock_VoucherIn> GetList(string _searchstring)
       {
           //search theo tieu chi nao do         
           try
           {
               return entity.GetBySearchString(_searchstring, Table.Stock_VoucherIn.ToString()).ToList();
           }
           catch { return null; }
       }


       /// <summary>
       /// Delete
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool Delete(object _model)
       {
           try
           {
               entity.Delete((CoreData.Stock_VoucherIn)_model, Table.Stock_VoucherIn.ToString());
               return true;
           }
           catch { return false; }
       }

       /// <summary>
       /// Delete a object by hidden
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool UpdateIsDelete(object _model)
       {
           try
           {
               var obj = (CoreData.Stock_VoucherIn)_model;
               obj.IsDelete = true;
               entity.Update(obj, Table.Stock_VoucherIn.ToString());
               return true;
           }
           catch { return false; }
       }
       #endregion



        #region Other Method

       public string GetVoucherNo(string _Prefix)
       {
           //lay ma phieu tu dong        
           try
           {
               return entity.GetByCusTomSQL(String.Format(StrGetVoucherNo,_Prefix)).FirstOrDefault().VoucherInNo;
           }
           catch { return null; }
       }
       /// <summary>
       /// Get List with search string
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Stock_VoucherIn> GetListByFilter(string _searchstring)
       {
           //search theo tieu chi nao do         
           try
           {
               //string _searchstring = "SV.ID>"+_data.ID.ToString();
               return entity.GetByCusTomSQL(String.Format(StrGetVoucher, _searchstring)).ToList();
           }
           catch { return null; }
       }
       /// <summary>
       /// Get  with search string
       /// </summary>
       /// <returns></returns>
       public CoreData.Stock_VoucherIn GetVoucherByFilter(Stock_VoucherIn _data)
       {
           //search theo tieu chi nao do         
           try
           {
               string _searchstring = "SV.ID =" + _data.ID.ToString();
               return entity.GetByCusTomSQL(String.Format(StrGetVoucher, _searchstring)).FirstOrDefault();
           }
           catch { return null; }
       }

        #endregion 
    
    }
         
}
